import React, { useEffect, useState } from 'react';
import { Button, message, Modal, Spin } from 'antd';
const Update = () => {
  const [update, setUpdate] = useState();

  useEffect(() => {
    window.registerEvent?.('checkUpdate-reply', (result) => {
      const status = result.status;
      console.log('status, result =>', status, result);
      if (status === 'error') {
        message.error(`error :${result.message}`);
        return;
      }

      if (status === 'update-available') {
        Modal.confirm({
          title: '有可用更新',
          content: `新版本${'v1.0.1'}`,
          onOk: () => {
            window.useElectronApi?.confirmDownloadUpdate?.();
          },
        });
        return;
      }

      if (status === 'update-not-available') {
        message.info('已经是最新版本');
        return;
      }

      if (status === 'download-progress') {
        setUpdate(result);
        return;
      }

      if (status === 'update-downloaded') {
        Modal.confirm({
          title: '下载完成',
          content: '是否立即更新?',
          onOk: () => {
            window.useElectronApi?.confirmUpdate?.();
          },
        });
        return;
      }
    });
  });

  const handleCheckUpdate = () => {
    window.useElectronApi?.checkUpdate?.();
  };

  return (
    <div>
      <Spin spinning={false} tip={<div>100%</div>}>
        <Button onClick={handleCheckUpdate}>Check Update</Button>
      </Spin>
    </div>
  );
};
export default Update;
